package com.abcdefg.rabbitmqextensionsdemo.controller;

import com.abcdefg.rabbitmqextensionsdemo.constants.Constant;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.Date;

@RestController
@RequestMapping("/produce")
public class RabbitMqController {
    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;

//    @Resource(name = "traceRabbitTemplate")
//    private RabbitTemplate traceRabbitTemplate;

    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constant.DIRECT_EXCHANGE_NAME,
                "ack",
                "producer ack test....");
        return "发送成功";
    }

    @RequestMapping("/pre")
    public String pre() {
        // 要发送的消息内容
        String message = "This is a persistent message";

        // 创建⼀个Message对象，设置为持久化
        Message messageObject = new Message(message.getBytes(), new MessageProperties());
        //设置消息非持久化
        messageObject.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        //设置消息持久化
        messageObject.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        rabbitTemplate.convertAndSend(Constant.PRES_EXCHANGE_NAME, "pre", messageObject);
        return "持久化消息发送成功！";

        //这个是在JDK代码中实现的消息非持久化
//        //⾮持久化信息
//         channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
//
//         //持久化信息
//         channel.basicPublish("",QUEUE_NAME,
//                 MessageProperties.PERSISTENT_TEXT_PLAIN,msg.getBytes());
    }


    //发送方确认
    @RequestMapping("confirm")
    public String confirm() {
        CorrelationData correlationData = new CorrelationData("1");
        confirmRabbitTemplate.convertAndSend(Constant.CONFIRM_EXCHANGE, "confirm111",
                "confirm test...", correlationData);
        return "发送方消息确认！";
    }

    //消息被退回
    @RequestMapping("returns")
    public String returns() {
        CorrelationData correlationData = new CorrelationData("2");
        confirmRabbitTemplate.convertAndSend(Constant.CONFIRM_EXCHANGE, "confirm111",
                "confirm test...", correlationData);
        return "消息被退回！";
    }


    //重试机制
    @RequestMapping("retry")
    public String retry() {
        rabbitTemplate.convertAndSend(Constant.RETRY_EXCHANGE, "retry", "retry test...");
        return "测试重试机制";
    }


    //ttl机制
    @RequestMapping("ttl")
    public String ttl() {
//        MessagePostProcessor messagePostProcessor=new MessagePostProcessor() {
//            @Override
//            public Message postProcessMessage(Message message) throws AmqpException {
//                message.getMessageProperties().setExpiration("10000");//设置消息过期时间10s
//                return message;
//            }
//        };
        rabbitTemplate.convertAndSend(Constant.TTL_EXCHANGE, "ttl", "ttl test...",
                messagePostProcessor -> {
                    messagePostProcessor.getMessageProperties().setExpiration("50000");//设置消息过期时间10s
                    return messagePostProcessor;
                });

        rabbitTemplate.convertAndSend(Constant.TTL_EXCHANGE, "ttl", "ttl test...",
                messagePostProcessor -> {
                    messagePostProcessor.getMessageProperties().setExpiration("20000");//设置消息过期时间30s
                    return messagePostProcessor;
                });
        return "测试ttl机制";
    }

    @RequestMapping("ttl2")
    public String ttl2() {
        rabbitTemplate.convertAndSend(Constant.TTL_EXCHANGE, "ttl", "ttl2 test...");
        return "测试ttl2机制";
    }


    @RequestMapping("dl")
    public String dl() {
        rabbitTemplate.convertAndSend(Constant.NORMAL_EXCHANGE, "normal", "dl test...");
        System.out.printf("%tc 接收到的消息：\n", new Date());
//        for (int i = 0; i < 20; i++) {
//            rabbitTemplate.convertAndSend(Constant.NORMAL_EXCHANGE,"normal","dl test..."+i);
//        }
        return "测试死信机制";
    }


//    @RequestMapping("delay")
//    public String delay(){
//        rabbitTemplate.convertAndSend(Constant.NORMAL_EXCHANGE,"normal","ttl2 test...",
//                messagePostProcessor->{
//                    messagePostProcessor.getMessageProperties().setExpiration("10000");//设置消息过期时间10s
//                    return  messagePostProcessor;
//                });
//        rabbitTemplate.convertAndSend(Constant.NORMAL_EXCHANGE,"normal","ttl1 test...",
//                messagePostProcessor->{
//                    messagePostProcessor.getMessageProperties().setExpiration("30000");//设置消息过期时间30s
//                    return  messagePostProcessor;
//                });
//
//        System.out.printf("%tc 发送消息成功 \n",new Date());
//        return "测试delay机制";
//    }

    @RequestMapping("delay")
    public String delay() {

        rabbitTemplate.convertAndSend(Constant.DELAY_EXCHANGE, "delay", "delay 30s test...",
                messagePostProcessor -> {
                    messagePostProcessor.getMessageProperties().setDelayLong(30000L);
                    return messagePostProcessor;
                });
        rabbitTemplate.convertAndSend(Constant.DELAY_EXCHANGE, "delay", "delay 10s test...",
                messagePostProcessor -> {
                    messagePostProcessor.getMessageProperties().setDelayLong(10000L);
                    return messagePostProcessor;
                });


        System.out.printf("%tc 发送消息成功 \n", new Date());
        return "测试delay插件";
    }


    //    @Transactional
    @Transactional
    @RequestMapping("trace")
    public String trace() {
        rabbitTemplate.convertAndSend("", Constant.TRACE_QUEUE, "trace1 test...");
//        int a = 10 / 0;
        rabbitTemplate.convertAndSend("", Constant.TRACE_QUEUE, "trace2 test...");
        return "trace测试，消息发送成功！";
    }



    @RequestMapping("qus")
    public String qus() {
        for (int i = 0; i < 30; i++) {
            rabbitTemplate.convertAndSend(Constant.QUS_EXCHANGE, "qus", "qus test..."+i);
        }
        return "测试消息分发!消息分发成功!";
    }
}
